package org.rslai.tcedit.web.testCaseMgt;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.validator.InvalidValue;
import org.rslai.tcedit.domain.Catalog;
import org.rslai.tcedit.domain.Version;
import org.rslai.tcedit.exception.ApplicationException;
import org.rslai.tcedit.util.HttpUtil;
import org.rslai.tcedit.util.SessionUtil;
import org.rslai.tcedit.web.core.AbstractMvcController;

/**
 * 测试用例分类编写
 * 
 * @author rslai
 * 
 * @spring.bean name="/testCaseMgt/catalogForm.jspx"
 */
public class catalogFormController extends AbstractMvcController {

	@Override
	public Map<String, Object> getModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = request.getMethod();
		Map<String, Object> map = new HashMap<String, Object>();
		Catalog catalog = null;
		Catalog parentCatalog = null;

		String action = HttpUtil.getString(request, "action", "").toLowerCase();
		String catalogID = HttpUtil.getString(request, "id", "");

		try {
			if (action.equals("delete")) { // 删除
				catalog = (Catalog) catalogService.query(SessionUtil.getCurrentProductID(request), Catalog.class, catalogID);
				catalogService.delete(SessionUtil.getCurrentProductID(request), catalog);
				map.put("refreshTree", "delete"); // 刷新 tree
				map.put("close", false); // 关闭弹出窗口
				catalog = null;
			}else if (method.equals("GET")) {
				if  (catalogID.isEmpty()) { // 新增（返回空对象） 
					catalog = new Catalog();

					String pid = HttpUtil.getString(request, "pid", "");
					if (pid.equals("")) {
						parentCatalog = new Catalog();
						parentCatalog.setId(Catalog.ROOT_ID);
						parentCatalog.setName(Catalog.ROOT_NAME);
					} else {
						parentCatalog = (Catalog) catalogService.query(SessionUtil.getCurrentProductID(request), Catalog.class, pid);
					}
				} else { // 查询
					catalog = (Catalog) catalogService.query(SessionUtil.getCurrentProductID(request), Catalog.class, catalogID);

					if (catalog.getParentCatalogID().equals(Catalog.ROOT_ID)) {
						parentCatalog = new Catalog();
						parentCatalog.setId(Catalog.ROOT_ID);
						parentCatalog.setName(Catalog.ROOT_NAME);
					} else {
						parentCatalog = (Catalog) catalogService.query(SessionUtil.getCurrentProductID(request), Catalog.class, catalog.getParentCatalogID());
					}
				}
			}else if(method.equals("POST")) {
				if (action.equals("create")) { // 新增
					catalog = (Catalog) HttpUtil.createFormBean(request, Catalog.class);
					catalog.setVersion((Version) versionService.query(Version.class, SessionUtil.getCurrentVersionID(request)));
					catalog.setParentCatalogID(HttpUtil.getString(request, "parentCatalogID", ""));
					catalogService.create(SessionUtil.getCurrentProductID(request), catalog);
					map.put("refreshTree", "create"); // 刷新 tree
					map.put("close", true); // 关闭弹出窗口
				} else if (action.equals("update")) { // 修改
					Catalog formCatalog = (Catalog) HttpUtil.createFormBean(request, Catalog.class);
					catalog = (Catalog) catalogService.query(SessionUtil.getCurrentProductID(request), Catalog.class, catalogID);
					catalog.setParentCatalogID(HttpUtil.getString(request, "parentCatalogID", ""));
					catalog.setName(formCatalog.getName());
					catalog.setDescribing(formCatalog.getDescribing());
					catalogService.update(catalog);
					map.put("refreshTree", "update"); // 刷新 tree
					map.put("close", true); // 关闭弹出窗口
				}
			}
		} catch(Exception e) {
			map.put("error", convertException(e));
		}

		map.put("action", action);
		map.put("ROOT_ID", Catalog.ROOT_ID);
		map.put("ROOT_NAME", Catalog.ROOT_NAME);
		map.put("catalog", catalog);
		map.put("parentCatalog", parentCatalog);

		return map;
	}

	@Override
	public String getView(HttpServletRequest request, HttpServletResponse response) {
		return "/testCaseMgt/catalogForm.htm";
	}

	private String convertException(Exception e) throws Exception {
		if (e instanceof ApplicationException) {
			return e.getMessage();
		} else if (e instanceof InvalidStateException) {
			StringBuffer sb = new StringBuffer(128);
			InvalidValue[] ivs = ((InvalidStateException) e).getInvalidValues();
			for (InvalidValue iv : ivs)
				sb.append(iv.getMessage()).append("<br/>");
			return sb.toString();
		}
		throw e;
	}

}
